package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public PageResponse<NursingPlanVo> getNursingPlanPageList(NursingPlanDto nursingPlanDto) {
        PageHelper.startPage(nursingPlanDto.getPageNum(), nursingPlanDto.getPageSize());
        Page<NursingPlanVo> pageList = nursingPlanMapper.getNursingPlanPageList(nursingPlanDto);
        return PageResponse.of(pageList,NursingPlanVo.class);
    }

    @Override
    public List<NursingPlanVo> getNursingPlanList() {
        return nursingPlanMapper.getNursingPlanList();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan1 = nursingPlanMapper.getNursingPlanByName(nursingPlanDto.getPlanName());
        if (nursingPlan1 != null) {
            throw new BaseException(BasicEnum.NURSING_PLAN_NAME_REPERT);
        }
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.addNursingPlan(nursingPlan);
        List<NursingProjectPlan> collect = nursingPlanDto.getProjectPlans().stream().map(nursingProjectPlanDto -> {
            NursingProjectPlan nursingProjectPlan = BeanUtil.copyProperties(nursingProjectPlanDto, NursingProjectPlan.class);
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            return nursingProjectPlan;
        }).collect(Collectors.toList());
        nursingProjectPlanMapper.addNursingProjectPlan(collect);
    }

    @Override
    public void updateNursingPlanStatusById(Long id, Integer status) {
        nursingPlanMapper.updateNursingPlanStatusById(id,status);
    }

    @Override
    public void deleteNursingPlanById(Long id) {
        List<NursingProjectPlanVo> nursingProjectPlan = nursingProjectPlanMapper.getNursingProjectPlanByPlanId(id);
        if (nursingProjectPlan != null && !nursingProjectPlan.isEmpty()) {
            throw new BaseException(BasicEnum.NURSING_PLAN_CONNECTION);
        }
        nursingPlanMapper.deleteNursingPlanById(id);
    }

    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getNursingPlanById(id);
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.getNursingProjectPlanByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateNursingPlanById(Long id,NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan1 = nursingPlanMapper.getNursingPlanByName(nursingPlanDto.getPlanName());
        if (nursingPlan1 != null && !nursingPlan1.getId().equals(id)) {
            throw new BaseException(BasicEnum.NURSING_PLAN_NAME_REPERT);
        }
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        nursingPlan.setId(id);
        nursingPlanMapper.updateNursingPlanById(nursingPlan);
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(id);
        List<NursingProjectPlan> collect = nursingPlanDto.getProjectPlans().stream().map(nursingProjectPlanDto -> {
            NursingProjectPlan nursingProjectPlan = BeanUtil.copyProperties(nursingProjectPlanDto, NursingProjectPlan.class);
            nursingProjectPlan.setPlanId(id);
            return nursingProjectPlan;
        }).collect(Collectors.toList());
        nursingProjectPlanMapper.addNursingProjectPlan(collect);
    }
}
